import {
	sub
} from '@/common/event.js'
import store from '../index.js'
import moment from 'moment'
import config from '../../config.js'
import { getRqst } from '../../common/request'
export default {
	namespaced: true,
	state: {
		user:{},
		systemName:"BGYX",
		openid:'',
		news:[],
		islogin:false,
		systemInfo:{
			fileurl:''
		},
		status:'normal'
	},
	mutations: {
		cleardata:(state, payload)=>{
			state.user = {}
			state.status = 'normal'
		},
		setSystemInfoItem:(state, payload)=>{
			state.systemInfo[payload.key] = payload['data']
		},
		setSystemInfo:(state, payload)=>{
			state.systemInfo = payload
		},
		setOpenid:(state, payload)=>{
			state.openid = payload
		},
		setLoginStatus:(state, payload)=>{
			state.islogin = payload
		},
		setNewsData:(state, payload)=>{
			state.news = payload
		},
		setStatus:(state, payload)=>{
			state.status = payload
		},
	},
	actions: {
		async init({
			commit,
			state
		}, {
			payload
		}) {
			const ret = await getRqst().get(`common/common/getSystemInfo`)
			commit(`setSystemInfo`,ret.data)
		}
	},
	init: async function() {
		store.commit(`common/setSystemInfoItem`,{
			key:'fileurl',
			data: config.fileurl
		})
		console.log("common初始化")
		store.dispatch({
			type:"common/init",
			payload:{}
		})
		
		sub("logout", function(e) {
			store.commit(`common/cleardata`, {})
		})
		
		sub("socket_sysinfochange",()=>{
			store.dispatch({
				type:"common/init",
				payload:{}
			})
		})
		
		
	}
}